package com.demostic.demostic_test.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

public class DecodePictureByUriUtils {
    public static Bitmap decodeUri(Context context, Uri uri, int maxWidth, int maxHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        resolveUri(context, uri, options);
        int scale = 1;
        for (int i = 0; i < Integer.MAX_VALUE && ((options.outWidth / scale > maxWidth && ((double) (options.outWidth / scale)) > ((double) maxWidth) * 1.4d) || (options.outHeight / scale > maxHeight && ((double) (options.outHeight / scale)) > ((double) maxHeight) * 1.4d)); i++) {
            scale++;
        }
        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        Bitmap bitmap = null;
        try {
            bitmap = resolveUriForBitmap(context, uri, options);
            if (bitmap != null) {
                Log.e("bitmap", "resolveUriForBitmap bitmap is null:" + bitmap);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    private static void resolveUri(Context context, Uri uri, BitmapFactory.Options options) {
        if (uri != null) {
            String scheme = uri.getScheme();
            if ("content".equals(scheme) || "file".equals(scheme)) {
                InputStream stream = null;
                try {
                    stream = context.getContentResolver().openInputStream(uri);
                    BitmapFactory.decodeStream(stream, null, options);
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e) {
                            Log.w("resolveUri", "Unable to close content: " + uri, e);
                        }
                    }
                } catch (Exception e2) {
                    Log.w("resolveUri", "Unable to open content: " + uri, e2);
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e3) {
                            Log.w("resolveUri", "Unable to close content: " + uri, e3);
                        }
                    }
                } catch (Throwable th) {
                    if (stream != null) {
                        try {
                            stream.close();
                        } catch (IOException e4) {
                            Log.w("resolveUri", "Unable to close content: " + uri, e4);
                        }
                    }
                    throw th;
                }
            } else if ("android.resource".equals(scheme)) {
                Log.w("resolveUri", "Unable to close content: " + uri);
            } else {
                Log.w("resolveUri", "Unable to close content: " + uri);
            }
        }
    }

    private static Bitmap resolveUriForBitmap(Context context, Uri uri, BitmapFactory.Options options) {
        Bitmap bitmap = null;
        if (uri != null) {
            bitmap = null;
            String scheme = uri.getScheme();
            if ("content".equals(scheme) || "file".equals(scheme)) {
                InputStream stream = null;
                try {
                    InputStream stream2 = context.getContentResolver().openInputStream(uri);
                    bitmap = BitmapFactory.decodeStream(stream2, null, options);
                    if (bitmap != null) {
                        Log.e("bitmap", "decodeStream bitmap is null:" + bitmap);
                    }
                    if (stream2 != null) {
                        try {
                            stream2.close();
                        } catch (IOException e) {
                            Log.w("resolveUriForBitmap", "Unable to close content: " + uri, e);
                        }
                    }
                } catch (Exception e2) {
                    Log.w("resolveUriForBitmap", "Unable to open content: " + uri, e2);
                    if (0 != 0) {
                        try {
                            stream.close();
                        } catch (IOException e3) {
                            Log.w("resolveUriForBitmap", "Unable to close content: " + uri, e3);
                        }
                    }
                } catch (Throwable th) {
                    if (0 != 0) {
                        try {
                            stream.close();
                        } catch (IOException e4) {
                            Log.w("resolveUriForBitmap", "Unable to close content: " + uri, e4);
                        }
                    }
                    throw th;
                }
            } else if ("android.resource".equals(scheme)) {
                Log.w("resolveUriForBitmap", "Unable to close content: " + uri);
            } else {
                Log.w("resolveUriForBitmap", "Unable to close content: " + uri);
            }
        }
        return bitmap;
    }
}
